package me.hetong.caigou.ui.view;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.json.JSONUtil;
import cn.hutool.log.StaticLog;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.Clipboard;
import javafx.scene.input.DataFormat;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebView;
import me.hetong.caigou.Global;
import me.hetong.caigou.entity.Zhao;
import me.hetong.caigou.ui.chessboard.ChessBoardWar;
import me.hetong.caigou.ui.chessboard.ChessboardForEngine;
import me.hetong.caigou.ui.chessboard.ChessboardWarOnlyBoard;
import me.hetong.caigou.ui.pane.PaneCommon;
import me.hetong.caigou.ui.stage.EditJuStage;
import me.hetong.caigou.ui.stage.IndexStage;
import me.hetong.caigou.util.DongPingQipuUtils;
import me.hetong.caigou.util.FxUtils;

import java.util.List;
import java.util.Objects;

import static me.hetong.caigou.Global.*;

/**
 * 推演页
 */
public class Infer {

    public static final WebView webView = LineChartWebView.init();
    //    public static Label information = FxUtils.createLabel("没有打开棋谱");
    public static ChessboardForEngine chessboardForEngine = new ChessboardForEngine(config.getGridSize());
    public static VBox matchBox = new VBox();
    private static double width = ChessboardWarOnlyBoard.CANVAS.getWidth() * 2 + config.getSpace();
    public static HBox topMenuBox = new HBox();

    public static Pane init(List<Pane> list) {

        VBox root = new VBox();
        root.setSpacing(config.getSpace());
        root.setAlignment(Pos.CENTER);
        root.setPadding(new Insets(config.getPadding()));
        PaneCommon.InferPane = root;

        webView.setPrefWidth(width);
        webView.setMaxWidth(width);
        webView.setMinWidth(width);

        root.getChildren().add(PaneCommon.matchInfoLabel);
        root.getChildren().add(createRow1());
        root.getChildren().add(webView);

        HBox root2 = new HBox();
        root2.setAlignment(Pos.CENTER);
        root2.setSpacing(config.getSpace());

//        {
//            Label label = new Label("南北对抗赛");
//            label.setStyle("-fx-font-weight: bold;-fx-text-fill: #fff;");
//            root2.getChildren().add(label);
//        }

        root2.getChildren().add(root);

        {
            // 创建右侧菜单 [ａ,ｂ,ｃ,ｄ,ｅ,ｆ,ｇ,ｈ,ｉ,ｇ,ｋ,ｌ,ｍ,ｎ,ｏ,ｐ,ｑ,ｒ,ｓ,ｔ,ｕ,ｖ,ｗ,ｘ,ｙ,ｚ]
            VBox vBox = new VBox();
            vBox.setAlignment(Pos.CENTER_LEFT);
            vBox.setSpacing(config.getSpace());
            vBox.getChildren().add(FxUtils.createLabelProIcon("打开ｏ", "fas-folder-open", event -> Global.openWen()));
            vBox.getChildren().add(FxUtils.createLabelProIcon("保存ｓ", "fas-save", event -> Global.saveWen()));
            vBox.getChildren().add(FxUtils.createLabelProIcon("粘贴ｐ", "fas-paste", event -> pastDongping()));
            vBox.getChildren().add(FxUtils.createLabelProIcon("创建ｃ", "", engine -> EditJuStage.init(null)));
            vBox.getChildren().add(FxUtils.createLabelProIcon("引擎ｅ", "fas-server", event -> engine.startUp()));
            vBox.getChildren().add(FxUtils.createLabelProIcon("分析ａ", "fas-radiation", event -> doInfer()));
            vBox.getChildren().add(FxUtils.createLabelProIcon("截图ｍ", "far-image", event -> Global.snapshot(PaneCommon.InferPane)));
            vBox.getChildren().add(FxUtils.createLabelProIcon("动图ｄ", "far-images", null));
            vBox.getChildren().add(FxUtils.createLabelProIcon("开局上", "far-images", event -> ChessBoardWar.begin()));
            vBox.getChildren().add(FxUtils.createLabelProIcon("终局下", "far-images", event -> ChessBoardWar.end()));
            vBox.getChildren().add(FxUtils.createLabelProIcon("回退左", "far-images", event -> ChessBoardWar.prev()));
            vBox.getChildren().add(FxUtils.createLabelProIcon("前进右", "far-images", event -> ChessBoardWar.next()));
            root2.getChildren().add(vBox);
        }


        VBox vBox = new VBox();
        vBox.setId("#ip");
        vBox.getChildren().

                add(root2);
        vBox.setVisible(false);
        vBox.setManaged(false);
        vBox.setAlignment(Pos.CENTER);

        list.add(vBox);
        return vBox;
    }

    /**
     * 分析棋局
     */
    public static void doInfer() {
        if (qipu != null && CollUtil.isNotEmpty(Global.qipu.getZhaoList())) {
            ThreadUtil.execute(() -> {
                for (Zhao zhao : qipu.getZhaoList()) {
                    try {
                        Thread.sleep(800);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    engine.search(zhao);
                }
            });
        } else {
            StaticLog.info("棋谱为空或者没有有效着法");
        }
    }

    private static HBox createRow2() {

        topMenuBox.setSpacing(config.getSpace());
        topMenuBox.setPrefWidth(width);
        topMenuBox.setMaxWidth(width);
        topMenuBox.setMinWidth(width);

        topMenuBox.setOnMouseEntered(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
//                vBox.setStyle("-fx-background-color: #98678799");
            }
        });
        topMenuBox.setOnMouseExited(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
//                vBox.setStyle("");
            }
        });

        topMenuBox.getChildren().add(FxUtils.createLabelPro("棋谱仓库m", event -> Left.switchToId("#pu")));

        topMenuBox.getChildren().add(FxUtils.createLabelPro("创建棋谱c", event -> {
            if (qipu == null) {
                EditJuStage.init(null);
            } else {
                EditJuStage.init(null);
            }
        }));

        topMenuBox.getChildren().add(FxUtils.createLabelPro("粘贴棋谱p", event -> {
            pastDongping();
        }));

        topMenuBox.getChildren().add(FxUtils.createLabelPro("保存棋谱s", event -> Global.saveWen()));

        return topMenuBox;
    }

    public static void pastDongping() {
        try {
            final Clipboard clipboard = Clipboard.getSystemClipboard();
            String content = (String) clipboard.getContent(DataFormat.PLAIN_TEXT);
            loadWen(DongPingQipuUtils.convert(content).toString());
            StaticLog.info("解析棋谱,{}", JSONUtil.toJsonStr(qipu));
        } catch (Exception e) {
            e.printStackTrace();
            IndexStage.VIEW.getMessageBean().info(e.getMessage());
            Global.playSound(Global.ERROR_AUDIO);
        }
    }

    private static HBox createRow1() {
        HBox hBox = new HBox();
        hBox.setSpacing(config.getSpace());
        hBox.setAlignment(Pos.CENTER);

        hBox.getChildren().add(chessboardForEngine);
//        hBox.getChildren().add(createMatchModel());
        hBox.getChildren().add(ChessBoardWar.ROOT);

        return hBox;
    }

    private static VBox createMatchModel() {
        matchBox.setVisible(false);
        matchBox.setManaged(false);
        matchBox.setSpacing(config.getSpace());
        matchBox.setMaxWidth(config.getMatchPanWidth());

        Label label = new Label("进入比赛模式");
        matchBox.getChildren().add(label);

        return matchBox;
    }

    public static void test() {

        if (!matchBox.getChildren().isEmpty()) {
            matchBox.getChildren().removeIf(Objects::nonNull);
        }

        Image image = new Image("file:/Users/hetong/Downloads/iShot_2023-08-06_20.38.35.png");

        ImageView imageView = new ImageView();
        imageView.setFitWidth(config.getMatchPanWidth());
        imageView.setFitHeight(image.getHeight() * config.getMatchPanWidth() / image.getWidth());
        imageView.setImage(image);

        matchBox.getChildren().add(imageView);

        Label label1 = FxUtils.createLabelPro("截取棋盘", new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
            }
        });
        matchBox.getChildren().add(label1);

    }


    public static void switchModel(int model) {
        if (model == 1) {
            // 分析模式
//            ChessboardWarOnlyBoard.reSize(40);
            chessboardForEngine.setVisible(true);
            chessboardForEngine.setManaged(true);
            webView.setVisible(true);
            webView.setManaged(true);
            matchBox.setVisible(false);
            matchBox.setManaged(false);
            IndexStage.VIEW.getMessageBean().info("切换到分析模式");
        } else if (model == 2) {
            // 讲棋模式
//            chessboardForEngine.setVisible(false);
//            chessboardForEngine.setManaged(false);
//            webView.setVisible(false);
//            webView.setManaged(false);
//            matchBox.setVisible(false);
//            matchBox.setManaged(false);
            IndexStage.VIEW.getMessageBean().info("切换到简洁模式");
//            ChessboardWarOnlyBoard.reSize(48);

        } else if (model == 3) {
            // 比赛模式
//            ChessboardWarOnlyBoard.reSize(40);
//            chessboardForEngine.setVisible(false);
//            chessboardForEngine.setManaged(false);
//            webView.setVisible(true);
//            webView.setManaged(true);
//            matchBox.setVisible(true);
//            matchBox.setManaged(true);
//            IndexStage.VIEW.getMessageBean().info("切换到赛事跟踪模式");
        }

//        ChessboardWarImage.initChessList(FenUtils.chessListToFen(ChessboardWarImage.chessList));
    }
}
